大家好,我是脚丫先生 (o^^o)
学了那么多的Docker知识,如果这节还是讲原理方面的知识,那么就过于纸上谈兵了,是时候开始真正的亮剑。(有参考其他博主的文章)
Docker的官方文档非常的详细,关于所有的命令行使用说明/规范,可以参考 https://docs.docker.com/reference/
docker version
查看docker容器信息
docker info
查看docker容器帮助
docker --help
提示:对于镜像的操作可使用镜像名、镜像长ID和短ID。
列出本地images
docker images
含中间映像层
docker images -a
只显示镜像ID
docker images -q
含中间映像层
docker images -qa
显示镜像摘要信息(DIGEST列)
docker images --digests
显示镜像完整信息
docker images --no-trunc
显示指定镜像的历史创建;参数:-H 镜像大小和日期,默认为true;--no-trunc 显示完整的提交记录;-q 仅列出提交记录ID
docker history -H redis
搜索仓库MySQL镜像
docker search mysql
##--filter=stars=600:只显示 starts>=600 的镜像
docker search --filter=stars=600 mysql
## --no-trunc 显示镜像完整 DESCRIPTION 描述
docker search --no-trunc mysql
## --automated :只列出 AUTOMATED=OK 的镜像
docker search --automated mysql
下载Redis官方最新镜像,相当于:docker pull redis:latest
docker pull redis
下载仓库所有Redis镜像
docker pull -a redis
下载私人仓库镜像
docker pull bitnami/redis
单个镜像删除,相当于:docker rmi redis:latest
docker rmi redis
强制删除(针对基于镜像有运行的容器进程)
docker rmi -f redis
多个镜像删除,不同镜像间以空格间隔
docker rmi -f redis tomcat nginx
删除本地全部镜像
docker rmi -f $(docker images -q)
1、删除所有容器
docker rm `docker ps -a -q`
2、删除所有镜像
docker rmi `docker images -q`
3、按条件删除镜像
没有打标签
发现服务器里都none的镜像
docker images|grep none|awk '{print $3}'|xargs docker rmips:
docker images :镜像列表
|grep none : 筛选含 none 的行
|awk '{print $3}' :awk行处理器打印第三个参数(IMAGE ID)
|xargs : 参数列表转换
docker rmi : 镜像删除命令
镜像名包含关键字
docker rmi --force `docker images | grep doss-api | awk '{print $3}'` //其中doss-api为关键字
用户提交对容器的修改,并生成新的镜像。
docker commit -m="描述信息" -a="作者" 容器id 目标镜像名:[版本TAG]
1)编写dockerfile
在自己新建的dockers目录下新建dockerfile文件
cd /dockers
vi Dockerfile
2)构建docker镜像
在dockers目录下执行
docker build -t="镜像名:版本号" .
. 符号表示当前目录。
docker inspect 容器id
[{"Id": "9a1f7b30a10a33a36b7649e8f0992247c7767fccc2d253dfdf7a42b0c4cb1220","Created": "2020-11-08T06:03:48.598473909Z","Path": "/bin/bash","Args": [],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 45074,"ExitCode": 0,"Error": "","StartedAt": "2020-11-08T06:03:48.796392054Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566","ResolvConfPath": "/var/lib/docker/containers/9a1f7b30a10a33a36b7649e8f0992247c7767fccc2d253dfdf7a42b0c4cb1220/resolv.conf","HostnamePath": "/var/lib/docker/containers/9a1f7b30a10a33a36b7649e8f0992247c7767fccc2d253dfdf7a42b0c4cb1220/hostname","HostsPath": "/var/lib/docker/containers/9a1f7b30a10a33a36b7649e8f0992247c7767fccc2d253dfdf7a42b0c4cb1220/hosts","LogPath": "/var/lib/docker/containers/9a1f7b30a10a33a36b7649e8f0992247c7767fccc2d253dfdf7a42b0c4cb1220/9a1f7b30a10a33a36b7649e8f0992247c7767fccc2d253dfdf7a42b0c4cb1220-json.log","Name": "/jolly_mayer","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "default","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"CapAdd": null,"CapDrop": null,"Capabilities": null,"Dns": [],"DnsOptions": [],"DnsSearch": [],"ExtraHosts": null,"GroupAdd": null,"IpcMode": "private","Cgroup": "","Links": null,"OomScoreAdj": 0,"PidMode": "","Privileged": false,"PublishAllPorts": false,"ReadonlyRootfs": false,"SecurityOpt": null,"UTSMode": "","UsernsMode": "","ShmSize": 67108864,"Runtime": "runc","ConsoleSize": [0,0],"Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": null,"BlkioDeviceWriteBps": null,"BlkioDeviceReadIOps": null,"BlkioDeviceWriteIOps": null,"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"KernelMemory": 0,"KernelMemoryTCP": 0,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": false,"PidsLimit": null,"Ulimits": null,"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"LowerDir": "/var/lib/docker/overlay2/e9101448258930021351001b2ad30349f485f11800752a56c174cbf7ca0e0057-init/diff:/var/lib/docker/overlay2/2ffe0b620bf852329f5b2c405efb84cd6bbb1abbd75879276b4bed0e1962effc/diff","MergedDir": "/var/lib/docker/overlay2/e9101448258930021351001b2ad30349f485f11800752a56c174cbf7ca0e0057/merged","UpperDir": "/var/lib/docker/overlay2/e9101448258930021351001b2ad30349f485f11800752a56c174cbf7ca0e0057/diff","WorkDir": "/var/lib/docker/overlay2/e9101448258930021351001b2ad30349f485f11800752a56c174cbf7ca0e0057/work"},"Name": "overlay2"},"Mounts": [],"Config": {"Hostname": "9a1f7b30a10a","Domainname": "","User": "","AttachStdin": true,"AttachStdout": true,"AttachStderr": true,"Tty": true,"OpenStdin": true,"StdinOnce": true,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd": ["/bin/bash"],"Image": "centos","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.label-schema.build-date": "20200809","org.label-schema.license": "GPLv2","org.label-schema.name": "CentOS Base Image","org.label-schema.schema-version": "1.0","org.label-schema.vendor": "CentOS"}},"NetworkSettings": {"Bridge": "","SandboxID": "f125e682faaca30a5ff2ea78298b6a63aa4b416de1a4150dc2271de55bb2bec5","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {},"SandboxKey": "/var/run/docker/netns/f125e682faac","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "d49e14b7c195c2bb3d2ccad715cec0410d6b47e81fdec609296ac6ee84a47fb5","Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.2","IPPrefixLen": 16,"IPv6Gateway": "","MacAddress": "02:42:ac:11:00:02","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"NetworkID": "97d11e1f759394b549127015587527c5590965c6a66d0b1a5314dcf3b2ba626a","EndpointID": "d49e14b7c195c2bb3d2ccad715cec0410d6b47e81fdec609296ac6ee84a47fb5","Gateway": "172.17.0.1","IPAddress": "172.17.0.2","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "02:42:ac:11:00:02","DriverOpts": null}}}}
]
新建并启动容器,参数:-i 以交互模式运行容器;-t 为容器重新分配一个伪输入终端;--name 为容器指定一个名称
docker run -i -t --name mycentos
后台启动容器,参数:-d 已守护方式启动容器
docker run -d mycentos
注意:此时使用"docker ps -a"会发现容器已经退出。这是docker的机制:要使Docker容器后台运行,就必须有一个前台进程。解决方案:将你要运行的程序以前台进程的形式运行。
启动一个或多个已经被停止的容器
docker start redis
重启容器
docker restart redis
##top支持 ps 命令参数,格式:docker top [OPTIONS] CONTAINER [ps OPTIONS]
列出redis容器中运行进程
docker top redis
查看所有运行容器的进程信息
for i in `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done
查看redis容器日志,默认参数
docker logs rabbitmq
查看redis容器日志,参数:-f 跟踪日志输出;-t 显示时间戳;--tail 仅列出最新N条容器日志;
docker logs -f -t --tail=20 redis
查看容器redis从2019年05月21日后的最新10条日志。
docker logs --since="2019-05-21" --tail=10 redis
使用run方式在创建时进入
docker run -it centos /bin/bash
关闭容器并退出
exit
仅退出容器,不关闭
快捷键:Ctrl + P + Q
直接进入centos 容器启动命令的终端,不会启动新进程,多个attach连接共享容器屏幕,参数:--sig-proxy=false 确保CTRL-D或CTRL-C不会关闭容器
docker attach --sig-proxy=false centos
在 centos 容器中打开新的交互模式终端,可以启动新进程,参数:-i 即使没有附加也保持STDIN 打开;-t 分配一个伪终端
docker exec -i -t centos /bin/bash
以交互模式在容器中执行命令,结果返回到当前终端屏幕
docker exec -i -t centos ls -l /tmp
以分离模式在容器中执行命令,程序后台运行,结果不会反馈到当前终端
docker exec -d centos touch cache.txt
查看正在运行的容器
docker ps
查看正在运行的容器的ID
docker ps -q
查看正在运行+历史运行过的容器
docker ps -a
显示运行容器总文件大小
docker ps -s
查看容器的IP
docker inspect 容器ID | grep IPAddress
查看容器的环境变量
docker exec centos env
显示最近创建容器
docker ps -l
显示最近创建的3个容器
docker ps -n 3
不截断输出
docker ps --no-trunc
停止一个运行中的容器
docker stop redis
杀掉一个运行中的容器
docker kill redis
删除一个已停止的容器
docker rm redis
删除一个运行中的容器
docker rm -f redis删除所有的容器
docker rm -f $(docker ps -aq)
-l 移除容器间的网络连接,连接名为 db
docker rm -l db
-v 删除容器,并删除容器挂载的数据卷
docker rm -v redis
将redis容器中的文件copy至本地路径
docker cp redis:/[container_path] [local_path]
将主机文件copy至rabbitmq容器
docker cp [local_path] redis:/[container_path]/
将主机文件copy至redis容器,目录重命名为[container_path](注意与非重命名copy的区别)
docker cp [local_path] redis:/[container_path]
1、登录
http://xxxxx.com为私有仓库地址
docker login http://xxxxx.com
2、登录私有创建项目
例如项目叫:abc-dev
3、给镜像打tag
docker tag 2e25d8496557 xxxxx.com/abc-dev/arc:1334
2e25d8496557:IMAGE ID,可以用docker images 查看
xxxxx.com:私有hub域名
abc-dev:项目名称
arc:镜像名称
1334:镜像版本号
4、推送
假设有一服务名为AAA(基于镜像AAA)
步骤:
更新镜像
docker build -t AAA
stop容器
docker-compose stop AAA
删除容器
docker-compose rm -f AAA
基于更新后的镜像重新启动容器
docker-compose up -d AAA
docker push xxxxx.com/abc-dev/arc:1334
📢博客主页:https://blog.csdn.net/shujuelin
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!